#!/usr/bin/perl
# 
# Exercise 12.6
# 
# Write a program that uses Bioperl to perform a BLAST search at the NCBI web 
# site, then use Bioperl to parse the BLAST output.
#
# Answer to Exercise 12.6
#
# When you're using a module, the quickest way to proceed is to find a tutorial or examples
# in the module's documentation, and adapt the code you find there for your own purposes.
# So you may want to start by visiting http://www.bioperl.org (definitely if you need to
# install the BioPerl software first), and looking at the tutorial that is linked from that
# page.  Doing so, you'll probably (unless things have changed!) find that the tutorial shows
# you how to call the Blast parser; but for doing the BLAST search at NCBI you are advised
# to look at the example scripts run_blast_remote.pl and retrieve_blast.pl that come with
# the Bioperl source code in the "scripts" directory, and modify it accordingly.  And so I
# do here, but ...
#
# But I'm going to take a shortcut.  We've already got these programs that perform the BLAST
# search at NCBI, so why not just call them?   Alternatively, you could incorporate the code
# from those scripts.
#
# We notice that calling run_blast_remote.pl with our options creates the output file
# .blastn2.nr.temp.html (on Unix, that is).  This html # output shows the ID of the query that
# is put in the queue for later retrieval. 
#
# Setup: 
#
# There is some setup we have to do -- we have to get a copy of blast_config.pl and
# edit the line near the top as directed.  On my system I did this:
#	#$INSTALL_PATH = "/home/steve/perl/lib";
#	$INSTALL_PATH = "/usr/local/lib/perl5/site_perl/5.6.1";
#
# Unfortunately, we find that the version of the code we used, Bioperl0.7.2, has a bug in
# the script retrieve_blast.pl.  It seems that NCBI must have changed the format of the 
# html output it gives.  We find that we have to change the line

#	if($data =~ /RID" VALUE="(\S+)"/s) {  #"

#to 

#	if($data =~ /RID".*?VALUE="(\S+)"/is) {  #"

#(Without the comment # at the beginning, of course).

# We also find that we have to edit run_blast_remote.pl to point to blast_config.pl:

#	# Using blast_config.pl in the examples/blast distribution directory:
#	require "blast_config.pl"; 
#	# Proper path to blast_config.pl after you install it in your system:
#	#require "/home/steve/bin/bioperl/examples/blast/blast_config.pl";

# I've included these three files in the code for this chapter, so you should be able to leave
# them as you find them.  (Remember, you do have to install Bioperl too.)

# Now, all we have to do is 


`perl ./run_blast_remote.pl -db nr -prog blastn sample.fasta`;

wait_the_required_amount_of_time('.blastn2.nr.temp.html');

`perl ./retrieve_blast.pl .blastn2.nr.temp.html`;

print "The output is in the files sample_BLASTN.txt and sample_BLASTN.html\n";

# 
# The NCBI output suggests how long to wait before the results will be ready.
#
sub wait_the_required_amount_of_time {
	my($file) = '.blastn2.nr.temp.html';

	open(OUTFILE, $file) or die "Cannot open $file\n";

	my($html) = join('',<OUTFILE>);

	close (OUTFILE);

	$html =~ /The results are estimated to be ready in (\d+) seconds/si;
	my($seconds) = $1;
	# just to be on the safe side
	$seconds += 5;

	$| = 1;  # print immediately (otherwise the "sleep" might delay the print output)
	print "Waiting $seconds seconds ...";
	sleep($seconds);
	print "\n";
}
